<?php
class pro_sewa_gedung_model extends MY_Model {
	var $model = 'pro_sewa_gedung_model';

	function getApp() {
		$ls_sql	= "	SELECT	lokasi_tempat value,
							lokasi_tempat display
					FROM	pro_lokasi_tempat
					ORDER BY id_lokasi DESC";
					
		$result	= $this->db->query($ls_sql);
		if($result->num_rows() > 0){
			return $result->result_array();
		}else{
			return array();
		}
	}
	
	function GetJSON()
	{
		$ls_sql	= "	SELECT	a.id_tempat,a.no_toko,a.id_sewa,a.lokasi_tempat,a.luas_tempat,a.harga_sewa,a.status_sewa,a.id_lokasi
					FROM	pro_sewa_gedung a
					WHERE	(UPPER(a.id_tempat) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.no_toko) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.lokasi_tempat) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.luas_tempat) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.harga_sewa) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.status_sewa) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'a.id_lokasi' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order')).",REGEXP_REPLACE(a.no_toko, '[0-9]', ''), TO_NUMBER(REGEXP_SUBSTR(a.no_toko, '[0-9]+'))";	
		return json_encode( $this->paging($ls_sql) );
	}
	
	function GetJSONLOV()
	{
		$ls_sql	= "	SELECT	a.*
					FROM	pro_sewa_gedung a
					WHERE	UPPER(a.status_sewa)= 'BELUM DI SEWA' AND 
							(UPPER(a.id_tempat) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.no_toko) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.lokasi_tempat) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.luas_tempat) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.harga_sewa) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.status_sewa) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'a.id_lokasi' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));	
		return json_encode( $this->paging($ls_sql) );
	}

	function Add($arr_data){
		$this->db->insert('pro_sewa_gedung', $arr_data);
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('pro_sewa_gedung', $arr_data, $arr_rule);
	}
	
	function GetSingle($id_sewa){
		$result = $this->db->get_where('pro_sewa_gedung', array('id_sewa' => $id_sewa));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($id_sewa)
	{
		$result = $this->db->get_where('pro_sewa_gedung', array('id_sewa' => $id_sewa));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}

	function DeleteSingle($arr_rule)
	{
		try {
			if(	intval($this->get_data("SELECT COUNT(1) FROM pro_sewa_gedung WHERE id_sewa= '".$arr_rule['id_sewa']."'"))<=0)
			{
				return $this->error_delete.' [id: '.$arr_rule['id_sewa']."]<br />";
			}else{
				$this->db->delete('pro_sewa_gedung', array('id_sewa' => $arr_rule['id_sewa']));
				return '';
			}
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}
}
?>